Use case: DAC using timer trigger or external trigger

This use case demonstrates how to configure the DAC channel to convert data using a timer trigger or an external trigger.

Sequence diagram

DAC using timer trigger or external trigger

@startuml
participant "Application" as App
participant "DAC" as DAC

App -> DAC: HAL_DAC_Init()
App -> DAC: HAL_DAC_ConfigChannel()
group Optional [The calibration of DAC is Optional]
App -> DAC: HAL_DACEx_SelfCalibrate()
App -> DAC: HAL_DACEx_SetUserTrimming()
end
App -> DAC: HAL_DAC_SetValue()
App -> DAC: HAL_DAC_Start()
App -> DAC: HAL_DAC_Stop()

@enduml

Code snippet

HAL1

HAL2

HAL_DAC_Init(&hdac1);
DAC_ChannelConfTypeDef sConfig;
sConfig.DAC_HighFrequency = DAC_HIGH_FREQUENCY_INTERFACE_MODE_ABOVE_80MH;
sConfig.DAC_DMADoubleDataMode = DISABLE;
sConfig.DAC_SignedFormat = DISABLE;
sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
sConfig.DAC_Trigger = DAC_TRIGGER_T1_TRGO;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_EXTERNAL;
sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
sConfig.DAC_TrimmingValue = 31;
HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1);

/* The calibration of DAC is Optional */
HAL_DACEx_SelfCalibrate(&hdac1, &sConfig, DAC_CHANNEL_1);
HAL_DACEx_SetUserTrimming(&hdac1, &sConfig, DAC_CHANNEL_1, 31);

HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1,
                 DAC_ALIGN_12B_R, 2048);
HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
HAL_DAC_Stop(&hdac, DAC_CHANNEL_1);
hal_dac_config_t p_config;
hal_dac_channel_config_t p_config1;

HAL_DAC_Init(&hdac, HAL_DAC1);
p_config.high_frequency_mode = HAL_DAC_HIGH_FREQ_MODE_ABOVE_80MHZ;
HAL_DAC_SetConfig(&hdac, &p_config);

p_config1.data_sign_format = HAL_DAC_SIGN_FORMAT_UNSIGNED;
p_config1.data_trigger = HAL_DAC_TRIGGER_TIM1_TRGO;
p_config1.output_buffer = HAL_DAC_OUTPUT_BUFFER_ENABLED;
p_config1.output_connection = HAL_DAC_OUTPUT_CONNECTION_EXTERNAL;
p_config1.alignment = HAL_DAC_DATA_ALIGN_12_BITS_RIGHT;

HAL_DAC_SetConfigChannel(&hdac, HAL_DAC_CHANNEL_1,
                         &p_config1);

/* The calibration of DAC is Optional */
HAL_DAC_CalibrateChannelBuffer(&hdac, HAL_DAC_CHANNEL_1);
HAL_DAC_SetChannelBufferCalibrationValue(&hdac, HAL_DAC_CHANNEL_1, 31);

HAL_DAC_SetChannelAlignment(&hdac, HAL_DAC_CHANNEL_1,
                            HAL_DAC_DATA_ALIGN_12_BITS_RIGHT);
HAL_DAC_SetChannelData(&hdac, HAL_DAC_CHANNEL_1,
                       2048);

HAL_DAC_StartChannel(&hdac, HAL_DAC_CHANNEL_1);
HAL_DAC_StopChannel(&hdac, HAL_DAC_CHANNEL_1);

Key changes

  1. In HAL2, the channel trigger source is configured via the channel configuration structure.

  2. In HAL2, the DAC start/stop operations are performed per channel using HAL_DAC_StartChannel() and HAL_DAC_StopChannel().